package su.security.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import net.kuakao.core.dto.PagerDTO;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import su.base.Constants;
import su.base.dao.SuBaseDAO;
import su.security.dao.ISecurityModuleDAO;
import su.security.dao.ISecurityUrlDAO;
import su.security.entity.SecurityModule;
import su.security.entity.SecurityUrl;
import su.tool.SuIntUtils;


@Repository
public class SecurityUrlDAO extends SuBaseDAO implements ISecurityUrlDAO {
	@Autowired
	private ISecurityModuleDAO moduleDAO;
	
	public List<SecurityUrl> queryByPage(PagerDTO s)throws SQLException{
		List<SecurityUrl> urls = null;
		StringBuilder sql = new StringBuilder("select * from su_security_url where status=1");
		if(StringUtils.isNotBlank(s.getS1())){//id
			if(s.getS1().indexOf(",")>-1){
				sql.append(" and id in ("+s.getS1()+")");
			}else{
				sql.append(" and id="+s.getS1());
			}
			
		}
		if(StringUtils.isNotBlank(s.getS2())){//模块
			SecurityModule m = moduleDAO.getBaseinfo(SuIntUtils.getInt(s.getS2()));
			if(m!=null){
				String urlids = m.getUrlids();
				if(StringUtils.isNotBlank(urlids))sql.append(" and id in("+urlids+")");
			}
			
		}
		if(StringUtils.isNotBlank(s.getS3()))sql.append(" and isshow="+s.getS3());//是否显示
		sql.append(" ORDER BY id desc");
		
		return super.queryByPage(sql.toString(), new RowMapper<SecurityUrl>() {

			public SecurityUrl mapRow(ResultSet rs, int arg1) throws SQLException {
				SecurityUrl entity = new SecurityUrl();
				entity.setId(rs.getInt("id"));
				entity.setIsshow(rs.getInt("isshow"));
				entity.setStatus(rs.getInt("status"));
				entity.setUrl(rs.getString("url"));
				entity.setUrlname(rs.getString("urlname"));
				return entity;
			}
			
		}, s);
	}
	
	public SecurityUrl getBaseinfo(int id)throws SQLException{
		String sql = "select * from su_security_url where status=1 and id=?";
		return super.queryForObject(sql,new Object[]{id},new RowMapper<SecurityUrl>(){
			public SecurityUrl mapRow(ResultSet rs, int arg1) throws SQLException {
				SecurityUrl entity = new SecurityUrl();
				entity.setId(rs.getInt("id"));
				entity.setIsshow(rs.getInt("isshow"));
				entity.setStatus(rs.getInt("status"));
				entity.setUrl(rs.getString("url"));
				entity.setUrlname(rs.getString("urlname"));
				return entity;
			}
		});
	}
	
	public List<SecurityUrl> getAllUrl()throws SQLException{
		String sql = "select * from su_security_url where status=1 ORDER BY id desc";
		return super.query(sql.toString(), new RowMapper<SecurityUrl>() {

			public SecurityUrl mapRow(ResultSet rs, int arg1) throws SQLException {
				SecurityUrl entity = new SecurityUrl();
				entity.setId(rs.getInt("id"));
				entity.setIsshow(rs.getInt("isshow"));
				entity.setStatus(rs.getInt("status"));
				entity.setUrl(rs.getString("url"));
				entity.setUrlname(rs.getString("urlname"));
				return entity;
			}
			
		});
	}
	
	public List<SecurityUrl> getUrlList(String ids)throws SQLException{
		String sql = "select * from su_security_url where status=1 and id in ("+ids+") ORDER BY id";
		return super.query(sql.toString(), new RowMapper<SecurityUrl>() {

			public SecurityUrl mapRow(ResultSet rs, int arg1) throws SQLException {
				SecurityUrl entity = new SecurityUrl();
				entity.setId(rs.getInt("id"));
				entity.setIsshow(rs.getInt("isshow"));
				entity.setStatus(rs.getInt("status"));
				entity.setUrl(rs.getString("url"));
				entity.setUrlname(rs.getString("urlname"));
				return entity;
			}
			
		});
	}
	
	public List<SecurityUrl> getListByUrl(String url)throws SQLException{
		String sql = "select * from su_security_url where url=?";
		return super.query(sql,new Object[]{url}, new RowMapper<SecurityUrl>() {

			public SecurityUrl mapRow(ResultSet rs, int arg1) throws SQLException {
				SecurityUrl entity = new SecurityUrl();
				entity.setId(rs.getInt("id"));
				entity.setIsshow(rs.getInt("isshow"));
				entity.setStatus(rs.getInt("status"));
				entity.setUrl(rs.getString("url"));
				entity.setUrlname(rs.getString("urlname"));
				return entity;
			}
			
		});
	}
	
	public void insert(SecurityUrl o)throws SQLException{
		String sql = "insert into su_security_url(url,urlname,isshow,status) values(?,?,?,?)";
		super.update(sql, new Object[]{o.getUrl(),o.getUrlname(),o.getIsshow(),o.getStatus()});
	}
	
	public void update(SecurityUrl o)throws SQLException{
		String sql = "update su_security_url set url=?,urlname=?,isshow=?,status=? where id=?";
		super.update(sql, new Object[]{o.getUrl(),o.getUrlname(),o.getIsshow(),o.getStatus(),o.getId()});
	}
	
	public void remove(int id)throws SQLException{
		String sql = "update su_security_url set status=? where id=?";
		super.update(sql, new Object[]{Constants.STATUS_DISABLE,id});
	}
}

